#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
爬取 FastAPI 学习页面菜单链接的脚本
"""

import requests
from bs4 import BeautifulSoup
from typing import Dict

def scrape_fastapi_learn_tabs() -> Dict[str, str]:
    """
    爬取 FastAPI 学习页面的菜单链接
    
    Returns:
        Dict[str, str]: 菜单名称和链接的字典
    """
    url = "https://fastapi.tiangolo.com/zh/learn/"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }
    
    try:
        response = requests.get(url, headers=headers, timeout=10)
        response.raise_for_status()
        response.encoding = response.apparent_encoding
        
        soup = BeautifulSoup(response.text, 'html.parser')
        
        # 查找包含学习菜单的元素
        # 根据页面结构，学习菜单在 nav.md-nav--primary 下的 ul.md-nav__list 中
        nav_primary = soup.find('nav', class_='md-nav--primary')
        if not nav_primary:
            print("未找到主导航")
            return {}
        
        # 查找所有菜单链接
        menu_items = {}
        # 查找所有的 li 元素，其中包含导航链接
        nav_items = nav_primary.find_all('li', class_='md-nav__item')
        
        for item in nav_items:
            link = item.find('a', class_='md-nav__link')
            if link:
                # 获取链接文本和href属性
                span_text = link.find('span', class_='md-typeset')
                if span_text:
                    text = span_text.get_text(strip=True)
                else:
                    text = link.get_text(strip=True)
                
                href = link.get('href', '')
                
                # 只处理相对链接或以https://fastapi.tiangolo.com开头的链接
                if text and (href.startswith('/') or href.startswith('https://fastapi.tiangolo.com') or href.startswith('../')):
                    # 处理相对链接
                    if href.startswith('/'):
                        href = 'https://fastapi.tiangolo.com' + href
                    elif href.startswith('../'):
                        # 处理相对路径
                        href = 'https://fastapi.tiangolo.com' + href[2:]
                    
                    menu_items[text] = href
        
        return menu_items
        
    except requests.RequestException as e:
        print(f"请求错误: {e}")
        return {}
    except Exception as e:
        print(f"解析错误: {e}")
        return {}

def main():
    print("开始爬取 FastAPI 学习页面菜单...")
    menu_links = scrape_fastapi_learn_tabs()
    
    if menu_links:
        print("\n成功获取菜单链接:")
        print("-" * 50)
        for menu, link in menu_links.items():
            print(f"{menu}: {link}")
        print("-" * 50)
        print(f"总共获取到 {len(menu_links)} 个菜单项")
    else:
        print("未能获取到菜单链接")

if __name__ == "__main__":
    main()